<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Java爬虫多线程下载实例 | Program W</title><meta name="keywords" content="爬虫,多线程,下载"><meta name="author" content="Program W"><meta name="copyright" content="Program W"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="一、概述 大文件下载有许多方法，比如有idm，aria2，迅雷直链获取也可以在idm下载但是发现还是有些东西需要爬虫+批量（批量注定了数据不小，所依还得带上多线程），于是开始了尝试。  二、爬虫部分 爬虫对象  &#x2F;**爬虫实体 * @author LW * @2021&#x2F;10&#x2F;30 19:25 *&#x2F;@Data@NoArgsConstructor@AllArgsConstructorpublic cl">
<meta property="og:type" content="article">
<meta property="og:title" content="Java爬虫多线程下载实例">
<meta property="og:url" content="https://lwstudy.gitee.io/2022/05/06/my-down/index.html">
<meta property="og:site_name" content="Program W">
<meta property="og:description" content="一、概述 大文件下载有许多方法，比如有idm，aria2，迅雷直链获取也可以在idm下载但是发现还是有些东西需要爬虫+批量（批量注定了数据不小，所依还得带上多线程），于是开始了尝试。  二、爬虫部分 爬虫对象  &#x2F;**爬虫实体 * @author LW * @2021&#x2F;10&#x2F;30 19:25 *&#x2F;@Data@NoArgsConstructor@AllArgsConstructorpublic cl">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s2.loli.net/2022/05/06/eXjyW9FbK2LwxVG.jpg">
<meta property="article:published_time" content="2022-05-06T08:33:11.000Z">
<meta property="article:modified_time" content="2022-05-06T08:37:20.231Z">
<meta property="article:author" content="Program W">
<meta property="article:tag" content="爬虫">
<meta property="article:tag" content="多线程">
<meta property="article:tag" content="下载">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s2.loli.net/2022/05/06/eXjyW9FbK2LwxVG.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://lwstudy.gitee.io/2022/05/06/my-down/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: Program W","link":"链接: ","source":"来源: Program W","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Java爬虫多线程下载实例',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-05-06 16:37:20'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/spider.jpeg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">9</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">10</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">9</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首頁</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 其他</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li><li><a class="site-page child" href="/resources/"><i class="fa-fw fa-heartbeat"></i><span> 资源</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> One Piece</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://s2.loli.net/2022/05/06/eXjyW9FbK2LwxVG.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Program W</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首頁</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 其他</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li><li><a class="site-page child" href="/resources/"><i class="fa-fw fa-heartbeat"></i><span> 资源</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> One Piece</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Java爬虫多线程下载实例</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-05-06T08:33:11.000Z" title="发表于 2022-05-06 16:33:11">2022-05-06</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-05-06T08:37:20.231Z" title="更新于 2022-05-06 16:37:20">2022-05-06</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%88%AC%E8%99%AB/">爬虫</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%88%AC%E8%99%AB/%E5%A4%9A%E7%BA%BF%E7%A8%8B/">多线程</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%88%AC%E8%99%AB/%E5%A4%9A%E7%BA%BF%E7%A8%8B/%E4%B8%8B%E8%BD%BD/">下载</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Java爬虫多线程下载实例"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="一、概述"><a href="#一、概述" class="headerlink" title="一、概述"></a>一、概述</h1><blockquote>
<p>大文件下载有许多方法，比如有<code>idm</code>，<code>aria2</code>，迅雷直链获取也可以在<code>idm</code>下载<br>但是发现还是有些东西需要爬虫+批量（批量注定了数据不小，所依还得带上多线程），于是开始了尝试。</p>
</blockquote>
<h1 id="二、爬虫部分"><a href="#二、爬虫部分" class="headerlink" title="二、爬虫部分"></a>二、爬虫部分</h1><ul>
<li>爬虫对象</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**爬虫实体</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> LW</span></span><br><span class="line"><span class="comment"> * @2021/10/30 19:25</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="meta">@NoArgsConstructor</span></span><br><span class="line"><span class="meta">@AllArgsConstructor</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Parse</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String title;</span><br><span class="line">    <span class="keyword">private</span> String img;</span><br><span class="line">    <span class="keyword">private</span> String price;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>爬虫服务</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**爬虫服务</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> LW</span></span><br><span class="line"><span class="comment"> * @2021/10/31 14:35</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ParseService</span> </span>&#123;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line"><span class="comment">//测试数据----------https://www.945kmzh.com/pc</span></span><br><span class="line">        <span class="keyword">new</span> ParseService().parseTo945(<span class="string">&quot;xxx&quot;</span>).forEach(System.out::println);</span><br><span class="line">    &#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> List&lt;Parse&gt; <span class="title">parseTo945</span><span class="params">(String keywords)</span> <span class="keyword">throws</span> Exception</span>&#123;</span><br><span class="line">        String url = keywords;</span><br><span class="line">        <span class="comment">//解析网页   (Jsoup返回Document   就是页面对象  )</span></span><br><span class="line">        Document document = Jsoup.parse(<span class="keyword">new</span> URL(url),<span class="number">30000</span>);</span><br><span class="line">        Elements elements = document.getElementsByClass(<span class="string">&quot;post-item-thumbnail&quot;</span>);</span><br><span class="line"></span><br><span class="line">        ArrayList&lt;Parse&gt; goodsList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        <span class="keyword">for</span> (Element el : elements) &#123;</span><br><span class="line">            String img = el.getElementsByTag(<span class="string">&quot;img&quot;</span>).eq(<span class="number">0</span>).attr(<span class="string">&quot;src&quot;</span>);</span><br><span class="line"></span><br><span class="line">            Parse content = <span class="keyword">new</span> Parse();</span><br><span class="line">            content.setImg(img);</span><br><span class="line">            goodsList.add(content);</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> goodsList;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h1 id="三、多线程部分"><a href="#三、多线程部分" class="headerlink" title="三、多线程部分"></a>三、多线程部分</h1><ul>
<li>代码<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> LW</span></span><br><span class="line"><span class="comment"> * @2022/2/11 14:16</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyMultiThread</span> <span class="keyword">extends</span> <span class="title">Thread</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> List&lt;Parse&gt; list;<span class="comment">//集合</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> len;<span class="comment">//集合长度</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> n;<span class="comment">//线程数</span></span><br><span class="line">    <span class="keyword">private</span> String pre;<span class="comment">//路径前缀</span></span><br><span class="line">    <span class="keyword">private</span> String dir;<span class="comment">//目录</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> flag;<span class="comment">// 线程号</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">MyMultiThread</span><span class="params">(List&lt;Parse&gt; list, <span class="keyword">int</span> len, <span class="keyword">int</span> n, String pre, String dir, <span class="keyword">int</span> flag)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>();</span><br><span class="line">        <span class="keyword">this</span>.list = list;</span><br><span class="line">        <span class="keyword">this</span>.len = len;</span><br><span class="line">        <span class="keyword">this</span>.n = n;</span><br><span class="line">        <span class="keyword">this</span>.pre = pre;</span><br><span class="line">        <span class="keyword">this</span>.dir = dir;</span><br><span class="line">        <span class="keyword">this</span>.flag = flag;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">//n线程数</span></span><br><span class="line">        <span class="comment">// 每个线程执行一部分 (len/n)*flag---&gt;(len/n)*(flag+1)</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = (len/n)*flag; j &lt; (len/n)*(flag+<span class="number">1</span>); j++) &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;===线程&quot;</span>+flag+<span class="string">&quot;========&quot;</span>+(len/n)*flag+<span class="string">&quot;-&quot;</span>+(len/n)*(flag+<span class="number">1</span>)+<span class="string">&quot;===================&quot;</span>);</span><br><span class="line">            <span class="keyword">if</span>(list.get(j).getImg() != <span class="string">&quot;&quot;</span>) &#123;</span><br><span class="line">                String url = list.get(j).getImg();</span><br><span class="line">                System.out.println(url);</span><br><span class="line">                DownPic downPic = <span class="keyword">new</span> DownPic(url, pre + dir + j + <span class="string">&quot;.jpg&quot;</span>);</span><br><span class="line">                downPic.startDownload();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;===========================================&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="四、下载部分"><a href="#四、下载部分" class="headerlink" title="四、下载部分"></a>四、下载部分</h1></li>
<li>代码（主要是用于下载小文件）</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**单线程下载图片</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> LW</span></span><br><span class="line"><span class="comment"> * @2021/10/31 15:11</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DownPic</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String downloadUrl;<span class="comment">//下载的url地址</span></span><br><span class="line">    <span class="keyword">private</span> String name;<span class="comment">//文件名</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">DownPic</span><span class="params">(String downloadUrl, String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>();</span><br><span class="line">        <span class="keyword">this</span>.downloadUrl = downloadUrl;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 感觉还可以优化，直接把爬取的地址分几份，然后开启几个线程分别下载（下载大文件的情况）</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">startDownload</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//创建下载的url和保存文件</span></span><br><span class="line">            URL url = <span class="keyword">new</span> URL(downloadUrl);</span><br><span class="line">            URLConnection connection = url.openConnection();</span><br><span class="line">            <span class="keyword">int</span> contentLength = connection.getContentLength();</span><br><span class="line">            File file = <span class="keyword">new</span> File(name);</span><br><span class="line">            System.out.println(<span class="string">&quot;文件路径是: &quot;</span> + file.getAbsolutePath());</span><br><span class="line">            <span class="comment">//</span></span><br><span class="line">            RandomAccessFile fos = <span class="keyword">null</span>;</span><br><span class="line">            <span class="keyword">byte</span>[] buf = <span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">2048</span>];</span><br><span class="line">            BufferedInputStream bis = <span class="keyword">null</span>;</span><br><span class="line">            URLConnection conn;</span><br><span class="line"></span><br><span class="line">            conn = url.openConnection();</span><br><span class="line">            conn.setAllowUserInteraction(<span class="keyword">true</span>);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//获取随机读取的下载文件模式</span></span><br><span class="line">            fos = <span class="keyword">new</span> RandomAccessFile(file, <span class="string">&quot;rw&quot;</span>);</span><br><span class="line">            <span class="comment">//从网络流中读取数据，并写入到保存文件中</span></span><br><span class="line">            bis = <span class="keyword">new</span> BufferedInputStream(conn.getInputStream());</span><br><span class="line">            <span class="keyword">while</span>(<span class="keyword">true</span>)&#123;</span><br><span class="line">                <span class="keyword">int</span> len = bis.read(buf, <span class="number">0</span>, <span class="number">2048</span>);</span><br><span class="line">                <span class="keyword">if</span>(len == -<span class="number">1</span>)&#123;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                fos.write(buf, <span class="number">0</span>, len);</span><br><span class="line">            &#125;</span><br><span class="line">            bis.close();</span><br><span class="line">            fos.close();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (MalformedURLException e) &#123;</span><br><span class="line">            <span class="comment">// TODO Auto-generated catch block</span></span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            <span class="comment">// TODO Auto-generated catch block</span></span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h1 id="最后"><a href="#最后" class="headerlink" title="最后"></a>最后</h1><blockquote>
<p>代码就都在上面了。<code>\(^o^)/~</code>,代码仓库还有一些其他方式的爬虫尝试，以及一些文件操作、和粘贴板内容直接使用<br>代码地址：<a target="_blank" rel="noopener" href="https://gitee.com/lwstudy/my-down">https://gitee.com/lwstudy/my-down</a></p>
</blockquote>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Program W</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://lwstudy.gitee.io/2022/05/06/my-down/">https://lwstudy.gitee.io/2022/05/06/my-down/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://lwstudy.gitee.io" target="_blank">Program W</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E7%88%AC%E8%99%AB/">爬虫</a><a class="post-meta__tags" href="/tags/%E5%A4%9A%E7%BA%BF%E7%A8%8B/">多线程</a><a class="post-meta__tags" href="/tags/%E4%B8%8B%E8%BD%BD/">下载</a></div><div class="post_share"><div class="social-share" data-image="https://s2.loli.net/2022/05/06/eXjyW9FbK2LwxVG.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/05/06/learn-drools/"><img class="prev-cover" src="https://s2.loli.net/2022/05/06/wNBo1Zb9L3npHC8.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">规则引擎Drools学习与实例</div></div></a></div><div class="next-post pull-right"><a href="/2021/11/25/ry-vue-demo4/"><img class="next-cover" src="/img/default.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">若依RuoYi-Vue代码学习一---【DEBUG】登录验证时，Spring Security怎么帮我们查的用户信息</div></div></a></div></nav><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/spider.jpeg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Program W</div><div class="author-info__description">这是一次开始</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">9</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">10</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">9</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/lwstudy"><i class="fab fa-github"></i><span>Gitee</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/lwstudy" target="_blank" title="Gitee"><i class="fab fa-guilded"></i></a><a class="social-icon" href="https://blog.csdn.net/weixin_46037781" target="_blank" title="CSDN"><i class="fas fa-copyright"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">我们总得留下些什么吧</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E6%A6%82%E8%BF%B0"><span class="toc-number">1.</span> <span class="toc-text">一、概述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E7%88%AC%E8%99%AB%E9%83%A8%E5%88%86"><span class="toc-number">2.</span> <span class="toc-text">二、爬虫部分</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E5%A4%9A%E7%BA%BF%E7%A8%8B%E9%83%A8%E5%88%86"><span class="toc-number">3.</span> <span class="toc-text">三、多线程部分</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E4%B8%8B%E8%BD%BD%E9%83%A8%E5%88%86"><span class="toc-number">4.</span> <span class="toc-text">四、下载部分</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%9C%80%E5%90%8E"><span class="toc-number">5.</span> <span class="toc-text">最后</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/05/06/learn-drools/" title="规则引擎Drools学习与实例"><img src="https://s2.loli.net/2022/05/06/wNBo1Zb9L3npHC8.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="规则引擎Drools学习与实例"/></a><div class="content"><a class="title" href="/2022/05/06/learn-drools/" title="规则引擎Drools学习与实例">规则引擎Drools学习与实例</a><time datetime="2022-05-06T08:45:21.000Z" title="发表于 2022-05-06 16:45:21">2022-05-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/05/06/my-down/" title="Java爬虫多线程下载实例"><img src="https://s2.loli.net/2022/05/06/eXjyW9FbK2LwxVG.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java爬虫多线程下载实例"/></a><div class="content"><a class="title" href="/2022/05/06/my-down/" title="Java爬虫多线程下载实例">Java爬虫多线程下载实例</a><time datetime="2022-05-06T08:33:11.000Z" title="发表于 2022-05-06 16:33:11">2022-05-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/11/25/ry-vue-demo4/" title="若依RuoYi-Vue代码学习一---【DEBUG】登录验证时，Spring Security怎么帮我们查的用户信息"><img src="/img/default.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="若依RuoYi-Vue代码学习一---【DEBUG】登录验证时，Spring Security怎么帮我们查的用户信息"/></a><div class="content"><a class="title" href="/2021/11/25/ry-vue-demo4/" title="若依RuoYi-Vue代码学习一---【DEBUG】登录验证时，Spring Security怎么帮我们查的用户信息">若依RuoYi-Vue代码学习一---【DEBUG】登录验证时，Spring Security怎么帮我们查的用户信息</a><time datetime="2021-11-25T01:10:30.000Z" title="发表于 2021-11-25 09:10:30">2021-11-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/11/24/ry-vue-demo3/" title="若依RuoYi-Vue代码学习一---登录实现（验证码校验、token生成，token过滤【验证】）"><img src="https://s2.loli.net/2021/12/10/7jpGreZhuBQ9kLa.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="若依RuoYi-Vue代码学习一---登录实现（验证码校验、token生成，token过滤【验证】）"/></a><div class="content"><a class="title" href="/2021/11/24/ry-vue-demo3/" title="若依RuoYi-Vue代码学习一---登录实现（验证码校验、token生成，token过滤【验证】）">若依RuoYi-Vue代码学习一---登录实现（验证码校验、token生成，token过滤【验证】）</a><time datetime="2021-11-24T09:10:30.000Z" title="发表于 2021-11-24 17:10:30">2021-11-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/11/23/ry-vue-demo2/" title="若依RuoYi-Vue代码学习一---通用分页处理"><img src="https://s2.loli.net/2021/12/10/muYWxT7gpvj1LqB.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="若依RuoYi-Vue代码学习一---通用分页处理"/></a><div class="content"><a class="title" href="/2021/11/23/ry-vue-demo2/" title="若依RuoYi-Vue代码学习一---通用分页处理">若依RuoYi-Vue代码学习一---通用分页处理</a><time datetime="2021-11-23T09:23:30.000Z" title="发表于 2021-11-23 17:23:30">2021-11-23</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://s2.loli.net/2022/05/06/eXjyW9FbK2LwxVG.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2021 - 2022 By Program W</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">这里是底部了.请停止下降!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'zCYkwMBcHhcNvvPzdsooSPb1-gzGzoHsz',
      appKey: 'MxW3eBpKYaCGdvUxno6EkFuH',
      placeholder: '请留下你的足迹...',
      avatar: 'monsterid',
      meta: 'nick,mail,link'.split(','),
      pageSize: '10',
      lang: 'zh-CN',
      recordIP: false,
      serverURLs: '',
      emojiCDN: '',
      emojiMaps: "",
      enableQQ: false,
      path: window.location.pathname,
      requiredFields: ["nick,mail"],
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>